<?php
    session_start();
    include('../../configs/connect.php');
    mysql_query("SET NAMES 'UTF8'");
    $nguoilap=$_SESSION['ten_dn'];
    $thang=$_GET['thang'];
    $nam=$_GET['nam'];
    
    $sql_tong=" SELECT DISTINCT ma_donvi,ten_vt_nganh,madm_ttctlm,ngayhoc_ctlm,tbl_chitiet_lopmon.madm_ca,ten_mh,count(ma_dkm) as soluong
                FROM  tbl_lopmon,tbl_lopmon_monctdt,tbl_mon_ctdt,dm_monhoc,tbl_chitiet_lopmon,tbl_dangkymon,tbl_ctdt,dm_nganh
                WHERE tbl_lopmon.ma_lopmon = tbl_lopmon_monctdt.ma_lopmon
                AND   tbl_lopmon_monctdt.ma_mon_ctdt = tbl_mon_ctdt.ma_mon_ctdt
                AND   tbl_mon_ctdt.ma_monhoc=dm_monhoc.madm_mh
                AND   tbl_lopmon.ma_lopmon=tbl_chitiet_lopmon.ma_lopmon
                AND   tbl_lopmon.ma_lopmon=tbl_dangkymon.ma_lopmon
                AND   tbl_ctdt.ma_ctdt = tbl_mon_ctdt.ma_ctdt
                AND   tbl_ctdt.madm_nganh = dm_nganh.madm_nganh
                AND   (madm_ttctlm='thilan1' OR madm_ttctlm='thilan2')
                AND   ngayhoc_ctlm !=''
                AND   month(STR_TO_DATE(ngayhoc_ctlm,'%d/%m/%Y'))=$thang
                AND   year(STR_TO_DATE(ngayhoc_ctlm,'%d/%m/%Y'))=$nam
                GROUP BY ma_donvi,ten_vt_nganh,ngayhoc_ctlm,madm_ca,dm_monhoc.ten_mh
                ORDER BY ma_donvi,ten_vt_nganh";
    $ex_tinhtong=mysql_query($sql_tong);
    $i=1;
    
    $donvi=$nganh="";
    
    $sql_gopnhom="SELECT ma_donvi,ten_vt_nganh,count(ten_vt_nganh) gophang FROM ($sql_tong) as tonghop GROUP BY ma_donvi,ten_vt_nganh";
    $ex_gopnhom=mysql_query($sql_gopnhom);
    while($row=mysql_fetch_array($ex_gopnhom)){
        $ds_gop[]=$row;   
    }
    
    $sql_gopnhomdonvi="SELECT tbl_donvi.ma_donvi,ten_donvi,count(ten_vt_nganh) gophang FROM ($sql_tong) as tonghop,tbl_donvi WHERE tonghop.ma_donvi=tbl_donvi.ma_donvi GROUP BY ma_donvi";
    $ex_gopnhomdonvi=mysql_query($sql_gopnhomdonvi);
    while($row=mysql_fetch_array($ex_gopnhomdonvi)){
        $ds_gopdonvi[]=$row;   
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Kế hoạch</title>
<style type="text/css">
@charset "utf-8";
/* CSS Document */
*{
	margin:0;
	padding:0;
}
body{
	font-family:"Times New Roman", Times, serif;
	font-size:14px;
}
div{
	margin:10px 0px;
}
#container{
	width:210mm;
	margin:0px auto;
	padding:5px;
	
}
.bold{
	font-weight:bold;
}
.text-left,.ghichu{
	text-align:left;
}
.ghichu{
	font-size:10px;
}
.chuky{
	font-size:18px;
}
.width-20{
	width:20px;
}
.width-50{
	width:50px;
}
.width-70{
	width:70px;
}
.width-100{
	width:100px;
}
.width-130{
	width:130px;
}
.left-header{
	float:left;
	width:25%;
	padding-left:10px;
	text-align:center;
}
.right-header{
	text-align:center;
}
.tblkehoach{
	width:100%;
}
td,th{
	padding:3px;
	text-align:center;
}
.cantrai{
    text-align: left;   
}
.left-footer{
	float:left;
	width:30%;
	text-align:center;
}
.right-footer{
	float:right;
	width:30%;
	text-align:center;
}

</style>

</head>

<body>
	<div id="container">
    	<div class="header">
        	<div class="left-header">
            	<span>VIỆN ĐẠI HỌC MỞ HÀ NỘI <br /><b>KHOA ĐÀO TẠO TỪ XA</b></span>
            </div>
            <div class="right-header">
            	<span style="font-size:20px;"><b>SỐ LƯỢNG IN SAO ĐỀ THI HẾT MÔN, HẾT HỌC PHẦN</b></span><br />
                <b>(Tháng 12/2013)<br /></b>
            </div>
        </div>
        <div style="clear:both"></div>
        <div class="content">
        	<div>
            	<table class="tblkehoach" cellpadding="0" cellspacing="0" border="1">
                	<tr>
                    	<th  class="width-20">TT</th>
                        <th  class="width-70">Hội đồng thi</th>
                        <th  class="width-20">Lớp</th>
                        <th  class="width-20">Ngành học</th>
                        <th  class="width-20">Lần thi</th>
                        <th  class="width-70">Ngày thi</th>
                        <th  class="width-130">Môn thi</th>
                        <th  class="width-20">Phòng thi</th>
                    	<th  class="width-20">Số lượng</th>
                        <th  class="width-20">ghi chú</th>
                    </tr>
            <?php while($row=mysql_fetch_array($ex_tinhtong)){?>
                    <tr>
            <?php 
                    
                    if($donvi!=$row['ma_donvi']){
                        foreach($ds_gopdonvi as $key=>$value){
                            if($row['ma_donvi']==$value['ma_donvi']){
                                $gophang_dv=$value['gophang'];
                                break;
                            }
                        }
                     echo "<td rowspan='$gophang_dv'>".$i++."</td>";
                     echo "<td rowspan='$gophang_dv' class='width-70'>".$value['ten_donvi']."</td>";   
                    }  
                    
            ?>
                        <td></td>
            <?php 
                    if($donvi!=$row['ma_donvi'] || $nganh!=$row['ten_vt_nganh']){
                        $donvi=$row['ma_donvi'];
                        $nganh=$row['ten_vt_nganh'];
                        
                        foreach($ds_gop as $key=>$value){
                            if($row['ma_donvi']==$value['ma_donvi'] && $row['ten_vt_nganh']==$value['ten_vt_nganh']){
                                $gophang=$value['gophang'];
                                break;                                
                            }
                        }
                     
                     echo "<td rowspan='$gophang'>".$row['ten_vt_nganh']."</td>";   
                    }  
                    
            ?>                                                
                        <td><?php if($row['madm_ttctlm']=='thilan1')   echo "1" ;
                                  if($row['madm_ttctlm']=='thilan2')   echo "2" ;
                            ?>
                        </td>
                        <td><?php echo substr($row['ngayhoc_ctlm'],0,5)."(".$row['madm_ca'].")"?></td>
                        <td class="cantrai"><?php echo $row['ten_mh']?></td>                      
                        <td></td>
                        <td><?php echo $row['soluong']?></td>
                        <td></td>
                    </tr>
            <?php }?>
                </table>
            </div>
            <div style="padding-left:30px;">
            	<span><b></b></span><br />
                <span><i></i></span>
            </div>
        </div>
        <div class="footer">
        	<div class="left-footer">
            	<br />Người lập kế hoạch
                <br /><br /><br /><br /><br />
                <b class="chuky"><?php echo $nguoilap?></b>
            </div>
            <div class="right-footer">
            	<i>Hà Nội, ngày <?php echo date('d')?> tháng <?php echo date('m')?> năm <?php echo date('Y')?></i><br />
                <b>Ban chủ nhiệm khoa</b>
                <br /><br /><br /><br /><br />
                <b class="chuky"></b>
            </div>
        </div>
        <div style="clear:both"></div>
    </div>
</body>
</html>

